package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 三要素
@WebServlet("/addServlet")
public class AddServlet  extends HttpServlet {
    private BrandService brandService = new BrandService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 表单一定是post请求
        // 接收+封装 调用 响应
        // 接收封装 表单的数据，表单过来的什么数据？JSON数据，把JSON数据转对象
        req.setCharacterEncoding("utf-8"); // 注意中文问题
        Brand brand = JSON.parseObject(req.getInputStream(), Brand.class);
        System.out.println("表单数据:"+brand);
        // 调用
        boolean b = brandService.addPinPai(brand);
        if(b){
            // 回去的是JSON格式数据
            resp.getWriter().write("success");
        }else{
            resp.getWriter().write("error");
        }
    }
}
